﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Report._3._KeToan;
using CoreData;
using SchoolManagementSystem.Controls;
using DevExpress.XtraEditors.Controls;
namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class frmReportAccounting : DevExpress.XtraEditors.XtraForm
    {
        public frmReportAccounting()
        {
            InitializeComponent();
            
            SetDefault();

        }

        void SetDefault()
        {
            radioGroup1.SelectedIndex = 0;
            dateEdit_from.DateTime = DateTime.Now.AddMonths(-6);
            dateEdit_to.DateTime = DateTime.Now;
            buttonEdit_Student.Enabled = false;
       
        }

        long ClassiD;
        long CourseiD;
        long BranchiD;
        long StatusiD;
        long StudentiD;

      

        void ShowReport()
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                BaoCaoChiTietHocVien();
            }
            if (radioGroup1.SelectedIndex == 1)
            {
                BaoCaoTongHopHocVien();
            }
            if (radioGroup1.SelectedIndex == 2)
            {
                BaoCaoChiTietTheoLop();
            }
            if (radioGroup1.SelectedIndex == 3)
            {
                BaoCaoTongHopCacLopChuyenNghiep();
            }
            if (radioGroup1.SelectedIndex == 4)
            {
                BaoCaoTongHopCacLopNganHan();
            }
            if (radioGroup1.SelectedIndex == 5)
            {
                BaoCaoChiTietDongHocPhiNganHan();
            }
            if (radioGroup1.SelectedIndex == 6)
            {
                BaoCaoChiTietDongHocPhiChuyenNghiep();
            }
            if(radioGroup1.SelectedIndex==7)
            {
                BaoCaoTinhTrangDongHocPhi();
            }
        }

        void SelectBox()
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                buttonEdit_Branch.Enabled = true;
                buttonEdit_Course.Enabled = false;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = false;
                dateEdit_from.Enabled = true;
                dateEdit_to.Enabled = true;
                buttonEdit_Status.Enabled = false;
                
                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;

                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;
                lblstartdate.Text = "Nhập học từ";

            }
            if (radioGroup1.SelectedIndex == 1)
            {
                buttonEdit_Branch.Enabled = true;
                buttonEdit_Course.Enabled = true;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = true;
                dateEdit_from.Enabled = true;
                dateEdit_to.Enabled = true; buttonEdit_Status.Enabled = true;
               
                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;

                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;
                lblstartdate.Text = "Nhập học từ";

            }

            if (radioGroup1.SelectedIndex == 2)
            {
                buttonEdit_Branch.Enabled = false;
                dateEdit_from.Enabled = false;
                dateEdit_to.Enabled = false;
                buttonEdit_Course.Enabled = false;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = true; buttonEdit_Status.Enabled = false;
              
                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;

                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;
                lblstartdate.Text = "Nhập học từ";
            }
            if (radioGroup1.SelectedIndex == 3)
            {
                buttonEdit_Branch.Enabled = false;
                dateEdit_from.Enabled = true;
                dateEdit_to.Enabled = true;
                buttonEdit_Course.Enabled = false;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = false; buttonEdit_Status.Enabled = false;

                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;

                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;
                lblstartdate.Text = "Nhập học từ";
            }
            if (radioGroup1.SelectedIndex == 4)
            {
                buttonEdit_Branch.Enabled = false;
                dateEdit_from.Enabled = true;
                dateEdit_to.Enabled = true;
                buttonEdit_Course.Enabled = false;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = false; buttonEdit_Status.Enabled = false;

                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;

                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;
                lblstartdate.Text = "Nhập học từ";
            }
            if (radioGroup1.SelectedIndex == 5)
            {
                buttonEdit_Branch.Enabled = true;
                dateEdit_from.Enabled = true;
                dateEdit_to.Enabled = true;
                buttonEdit_Course.Enabled = true;
                buttonEdit_Student.Enabled = true;
                buttonEdit_Class.Enabled = true; buttonEdit_Status.Enabled = false ;

                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;

                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;
                lblstartdate.Text = "Nhập học từ";
            }
           
            if (radioGroup1.SelectedIndex == 6)
            {
                buttonEdit_Branch.Enabled = true;
                dateEdit_from.Enabled = true;
                dateEdit_to.Enabled = true;
                buttonEdit_Course.Enabled = true;
                buttonEdit_Student.Enabled = true;
                buttonEdit_Class.Enabled = true; buttonEdit_Status.Enabled = false;

                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;

                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

               
                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;
                lblstartdate.Text = "Nhập học từ";
            }
            if (radioGroup1.SelectedIndex == 7)
            {
                buttonEdit_Branch.Enabled = true;
                dateEdit_from.Enabled = true;
                dateEdit_to.Enabled = true;
                buttonEdit_Course.Enabled = true;
                buttonEdit_Student.Enabled = false;
                buttonEdit_Class.Enabled = true; buttonEdit_Status.Enabled = false;

                buttonEdit_Branch.EditValue = "";
                BranchiD = 0;

                buttonEdit_Class.EditValue = "";
                ClassiD = 0;

                buttonEdit_Course.EditValue = "";
                CourseiD = 0;

                buttonEdit_Status.EditValue = "";
                StatusiD = 0;
                lblstartdate.Text = "Đóng từ";
            }
        }

        private void simpleButton_View_Click(object sender, EventArgs e)
        {
            ShowReport();
        }

        void BaoCaoChiTietHocVien()
        {           
          //theo nganh
            BaoCaoChiTietHocVien ds = new BaoCaoChiTietHocVien();
            String search = "";
            if (BranchiD != 0)
                search += "Branch.ID=" + BranchiD +" and ";
            search += " Convert(varchar(11), Student.DateOfAdmission, 120) BETWEEN '" + dateEdit_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") + "'";
            var data = KeToanReportData.LoadStudentFeeBySearchString(search);
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();
            
        }

        void BaoCaoChiTietTheoLop()
        {

            BaoCaoChiTietTheoLop ds = new BaoCaoChiTietTheoLop();
            String search = "";
            if (ClassiD == 0)
            {
                MessageControl.ShowMessage("Chưa chọn lớp", "Error", MessageControl.Icon.Error);
                return;
            }
            search += "Class.ID=" + ClassiD ;
            //search += " Convert(varchar(11), Student.DateOfAdmission, 120) BETWEEN '" + dateEdit_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") + "'";
            var data = KeToanReportData.LoadStudentFeeBySearchString(search);
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();

        }

        void BaoCaoChiTietTheoNganh()
        {

            BaoCaoChiTietTheoNganh ds = new BaoCaoChiTietTheoNganh();
            String search = "";
            if (BranchiD == 0)
            {
                MessageControl.ShowMessage("Chưa chọn ngành", "Error", MessageControl.Icon.Error);
                return;
            }
            search += "Branch.ID=" + BranchiD +" and ";
            search += " Convert(varchar(11), Student.DateOfAdmission, 120) BETWEEN '" + dateEdit_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") + "'";
            var data = KeToanReportData.LoadStudentFeeBySearchString(search);
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();

        }

        void BaoCaoTongHopHocVien()
        {

            BaoCaoTongHopHocVien ds = new BaoCaoTongHopHocVien();
            String search = "";
            if (BranchiD != 0)
                search += "Branch.ID=" + BranchiD +" and ";
            if (ClassiD != 0)
                search += " Class.ID=" + ClassiD +" and ";
            if (CourseiD != 0)
                search += "Course.ID=" + CourseiD + " and ";
            if (StatusiD != 0)
                search += "Student.Status=" + StatusiD + " and ";
            search += " Convert(varchar(11), Student.DateOfAdmission, 120) BETWEEN '" + dateEdit_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") + "'";
            var data = KeToanReportData.LoadStudentFeeBySearchStringWithStatus(search);
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();

        }

        void BaoCaoTongHopCacLopChuyenNghiep()
        {

            BaoCaoTongHopChuyenNghiep ds = new BaoCaoTongHopChuyenNghiep();
            String search = "";
           
            search += " Class.Type=1 and Convert(varchar(11), Student.DateOfAdmission, 120) BETWEEN '" + dateEdit_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") + "'";
            var data = KeToanReportData.LoadStudentFeeBySearchStringWithStatus(search);
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();

        }

        void BaoCaoTongHopCacLopNganHan()
        {

            BaoCaoTongHopNganHan ds = new BaoCaoTongHopNganHan();
            String search = "";

            search += " Class.Type=0 and Convert(varchar(11), Student.DateOfAdmission, 120) BETWEEN '" + dateEdit_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") + "'";
            var data = KeToanReportData.LoadStudentFeeBySearchStringWithStatus(search);
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();

        }


        void BaoCaoChiTietDongHocPhiNganHan()
        {

            BaoCaoChiTietHocPhiNganHan ds = new BaoCaoChiTietHocPhiNganHan();
            String search = "";
            if (BranchiD != 0)
                search += "VwStudentInfo.BranchID=" + BranchiD + " and ";
            if (ClassiD != 0)
                search += " VwStudentInfo.ClassID=" + ClassiD + " and ";
            if (CourseiD != 0)
                search += "VwStudentInfo.CourseID=" + CourseiD + " and ";
           // if (StatusiD != 0)
              //  search += "Student.Status=" + StatusiD + " and ";
            if (StudentiD != 0)
            {
                search += "VwStudentInfo.StudentID=" + StudentiD + " and ";
            }
            search += " VwStudentInfo.ClassType =0 and Convert(varchar(11), VwStudentInfo.DateOfAdmission, 120) BETWEEN '" + dateEdit_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") + "'";

            var data = KeToanReportData.LoadStudentPayFee(search);
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();

        }

        void BaoCaoChiTietDongHocPhiChuyenNghiep()
        {

            BaoCaoChiTietHocPhiChuyenNghiep ds = new BaoCaoChiTietHocPhiChuyenNghiep();
            String search = "";
            if (BranchiD != 0)
                search += " VwStudentInfo.BranchID=" + BranchiD + " and ";
            if (ClassiD != 0)
                search += " VwStudentInfo.ClassID=" + ClassiD + " and ";
            if (CourseiD != 0)
            {
                search += "VwStudentInfo.CourseID=" + CourseiD + " and ";
               // var courfeetimebonlan = RegisterService.RegisterServices.Class_FeeService.GetFeeTimeByClassIDAndType(CourseiD, 4);
               // var courfeetimesaulan = RegisterService.RegisterServices.Class_FeeService.GetFeeTimeByClassIDAndType(CourseiD, 6);
               // ds.time4lan = courfeetimebonlan.StartDate1.ToShortDateString() + "," + courfeetimebonlan.StartDate2.ToShortDateString() + "," + courfeetimebonlan.StartDate3.ToShortDateString() + "," + courfeetimebonlan.StartDate4.ToShortDateString();
               // ds.time6lan = courfeetimesaulan.StartDate1.ToShortDateString() + "," + courfeetimesaulan.StartDate2.ToShortDateString() + "," + courfeetimesaulan.StartDate3.ToShortDateString() + "," + courfeetimesaulan.StartDate4.ToShortDateString() + "," + courfeetimesaulan.StartDate5.ToShortDateString() + "," + courfeetimesaulan.StartDate6.ToShortDateString();
            }
       
            if (StudentiD != 0)
            {
                search += "VwStudentInfo.StudentID=" + StudentiD + " and ";
            }
            search += " VwStudentInfo.ClassType =1 and Convert(varchar(11), VwStudentInfo.DateOfAdmission, 120) BETWEEN '" + dateEdit_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") + "'";
         
            var data = KeToanReportData.LoadStudentPayFee(search);
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();

        }

        /// <summary>
        /// Xem theo ngày từ và đến phải đóng, đã đóng, nợ
        /// </summary>
        void BaoCaoTinhTrangDongHocPhi()
        {
            BaoCaoTheoDoiHocPhi ds = new BaoCaoTheoDoiHocPhi();
            String search = "";
            if (BranchiD != 0)
                search += "VwStudentFee.BranchID=" + BranchiD + " and ";
            if (ClassiD != 0)
                search += " VwStudentFee.ClassID=" + ClassiD + " and ";
            if (CourseiD != 0)
                search += "VwStudentFee.CourseID=" + CourseiD + " and ";
          
            if (StudentiD != 0)
            {
                search += "VwStudentFee.ID=" + StudentiD + " and ";
            }
            if (search.Length > 0)
                search = search.Remove(search.Length - 4, 4);
            else search = "1=1";
            ds.StartDate = dateEdit_from.DateTime;
            ds.EndDate = dateEdit_to.DateTime;
            var data = KeToanReportData.LoadStudentFeeByStartDateEndDate(ds.StartDate,ds.EndDate,search, out ds.TotalHPPhaiThu, out ds.TotalHPDaThu, out ds.TotalHPNo);
           
            ds.DataSource = data;
            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();
        }


        private void buttonEdit_Class_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectClass selectclass = new frmSelectClass();
                if (selectclass.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + selectclass.Code + "] " + selectclass.Name;
                    buttonEdit_Class.EditValue = data;
                    ClassiD = selectclass.ID;
                }
                else
                {
                    buttonEdit_Class.EditValue = "";
                    ClassiD = 0;
                }
            }
            else {
                buttonEdit_Class.EditValue = "";
                ClassiD = 0;
            }
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectBox();
        }

        private void buttonEdit_Course_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectCourse select = new frmSelectCourse();
                if (select.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + select.Code + "] " + select.Name;
                    buttonEdit_Course.EditValue = data;
                    CourseiD = select.ID;
                }
                else
                {
                    buttonEdit_Course.EditValue = ""; CourseiD = 0;
                }
            }
            else { buttonEdit_Course.EditValue = ""; CourseiD = 0; }
        }

        private void buttonEdit_Branch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectBranch select = new frmSelectBranch();
                if (select.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + select.Code + "] " + select.Name;
                    buttonEdit_Branch.EditValue = data;
                    BranchiD = select.ID;
                }
                else
                {
                    buttonEdit_Branch.EditValue = ""; BranchiD = 0;
                }
            }
            else
            {
                buttonEdit_Branch.EditValue = ""; BranchiD = 0;
            }
        }

        private void buttonEdit_Status_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectStatus select = new frmSelectStatus(1);
                if (select.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + select.Code + "] " + select.Name;
                    buttonEdit_Status.EditValue = data;
                    StatusiD = select.ID;
                }
                else
                {
                    buttonEdit_Status.EditValue = ""; StatusiD = 0;
                }
            }
            else {
                buttonEdit_Status.EditValue = ""; StatusiD = 0;
            }
        }

        private void buttonEdit_Student_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectStudent select = new frmSelectStudent();
                if (select.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + select.Code + "] " + select.Name;
                    buttonEdit_Student.EditValue = data;
                    StudentiD = select.ID;
                }
                else
                {
                    buttonEdit_Student.EditValue = ""; StudentiD = 0;
                }
            }
            else
            {
                buttonEdit_Student.EditValue = ""; StudentiD = 0;
            }
        }

       

    
      

    }
}
